<?php


namespace app\common\library;


use hiredis;

class TokenModel {

    function create_xxx($id, $info, $ip, $expire) {
        $token = $id;
        //$token = $id;
        $info['id'] = $id;
        $info['ip'] = $ip;
        $info['expire'] = $expire;

        $key = 'TOKEN_' . $token;
        hiredis::db()->hMset($key, $info);
        hiredis::db()->expire($key, $expire);
        return $token;
    }

    function create($info, $ip, $expire) {
        $token = $info['id'] . "_" . strtoupper(uniqid());
        $info['ip'] = $ip;
        $info['expire'] = $expire;

        $key = 'TOKEN_' . $token;
        hiredis::db()->hMset($key, $info);
        hiredis::db()->expire($key, $expire);
        return $token;
    }

    function update_token($token, $kvs) {
        $key = 'TOKEN_' . $token;
        if (hiredis::db()->exists($key)) {
            hiredis::db()->hMset($key, $kvs);
        }
    }

    function expire($token, $expire) {
        $key = 'TOKEN_' . $token;
        hiredis::db()->expire($key, $expire);
    }

    function get_info($token) {
        $key = 'TOKEN_' . $token;
        $kvs = hiredis::db()->hGetAll($key);
        if (empty($key)) {
            return null;
        }

        return $kvs;
    }

    function delete($token) {
        $key = 'TOKEN_' . $token;
        $kvs = hiredis::db()->delete($key);
    }

}
